<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Thomas Breuss <thomas.breuss@zephir.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(t3lib_extMgm::extPath('zboard') . 'class.tx_zboard_controller.php');


/**
 * Plugin 'zBoard - Form' for the 'zboard' extension.
 *
 * @author	Thomas Breuss <thomas.breuss@zephir.ch>
 * @package	TYPO3
 * @subpackage	tx_zboard
 */
class tx_zboard_pi2 extends tx_zboard_controller
{
	var $prefixId      = 'tx_zboard_pi2';		// Same as class name
	var $scriptRelPath = 'pi2/class.tx_zboard_pi2.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'zboard';	// The extension key.

	public function init()
    {
        parent::init();
		$this->pi_USER_INT_obj = 1;

        $helper = $this->getHelper();
        if(!$helper->feuser()->loggedIn()) {
            $requestUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
            $feloginPid = intval($helper->config('feloginPid'));
            if($feloginPid > 0) {
                $helper->redirect()->toPage($feloginPid, array('redirect_url' => $requestUrl));
                exit;
            }
        }

    }

    public function defaultAction()
    {
        $pid = intval($this->getParam('pid', 0));
        $uid = intval($this->getParam('uid', 0));

        if(!$this->hasPermission()) {
            $this->view->pid = $pid;
            $this->view->uid = $uid;
            $this->render('error_permission');
            return;
        }

        $helper = $this->getHelper();
        $request = $this->getRequest();
        $gateway = new tx_zboard_gateway();
        $gateway->setAuthorField($helper->config('authorField'));

        $subject = trim($request->getPost('subject', ''));
        $message = trim($request->getPost('message', ''));
        $notify_me = intval($request->getPost('notify_me', 0));

        $this->view->uid = $uid;
        $this->view->subject = $subject;
        $this->view->message = $message;
        $this->view->notify_me = $notify_me;
        $this->view->params = $this->getParams();
        $this->view->rootline = $gateway->fetchRootline($helper->config('rootPid'), $pid);

        // Thema und Posts holen
        $uid = intval($this->getParam('uid', 0));
        if($uid > 0) {
            $topic = $gateway->fetchTopic($uid);
            $replies = $gateway->fetchReplies($uid);
            $replies->reverse();

            $this->view->topic = $topic;
            $this->view->replies = $replies;

        }

        $this->render('form');

	}

    public function saveAction()
    {
        $pid = $this->getParam('pid', 0);
        $uid = $this->getParam('uid', 0); // Parent UID

        if(!$this->hasPermission()) {
            $this->view->pid = $pid;
            $this->view->uid = $uid;
            $this->render('error_permission');
            return;
        }

        $helper = $this->getHelper();
        $request = $this->getRequest();

        // Validierung
        $subject = trim($request->getPost('subject', ''));
        $message = trim($request->getPost('message', ''));
        $notifyMe = intval($request->getPost('notify_me', 0));

        $errors = array();
        if(empty($pid)) {
            $errors['pid'] = 'Pid required';
        }
        if(empty($uid) && (empty($subject))) {
            $errors['subject'] = 'Subject required';
        }
        if(empty($message)) {
            $errors['message'] = 'Message required';
        }

        if(empty($errors)) {

            // Speichern
            $gateway = new tx_zboard_gateway();
            $gateway->setAuthorField($helper->config('authorField'));
            
            $feUserUid = $helper->feuser()->data('uid');

            if($uid == 0) {
                // Topic
                $gateway->insertTopic($pid, $subject, $message, $feUserUid, $notifyMe);
            } else {
                // Reply
                $gateway->insertReply($pid, $subject, $message, $uid, $feUserUid);
                $topic = $gateway->fetchTopic($uid);

                if(!empty($topic->notify_me) && t3lib_div::validEmail($topic->email)) {

                    $helper->url()->setUseAbsoluteUrl(true);

                    $this->initSnippet();
                    $this->snippet->author = $topic->author;
                    $this->snippet->topicId = $uid;
                    $this->snippet->subject = $subject;
                    $this->snippet->message = $message;
                    $this->snippet->topicUrl = $helper->url()->toPage($pid);

                    $helper->url()->setUseAbsoluteUrl(false);

                    $subject = $helper->config('notifyMeMailSubject');
                    $sender = $helper->config('notifyMeMailSender');
                    $message = $this->snippet->render('mailtext_new_post');
                    t3lib_div::plainMailEncoded($email, $subject, $message, 'From: ' . $sender);

                }
            }

            $helper->redirect()->toPage($pid);
            exit;
        }

        $this->view->errors = $errors;
        $this->defaultAction();

    }

    public function hasPermission()
    {
        $helper = $this->getHelper();
        $usergroup = $helper->config('usergroup');
        return (bool)$helper->feuser()->belongsTo($usergroup);
    }

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zboard/pi2/class.tx_zboard_pi2.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zboard/pi2/class.tx_zboard_pi2.php']);
}

?>